package com.wanmait.cookbook.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.demo.util.ImageUtil;

/**
 * Servlet implementation class CodeServlet
 */
/**
 * 	验证码Servlet
 * @author Jyf
 *
 */
@WebServlet("/CodeServlet")
public class CodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CodeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		if ("code".equals(action)) {
			this.code(request, response);
		}else if("checkCode".equals(action)) {
			this.checkCode(request, response);
		}else if ("check".equals(action)) {
		      this.check(request, response);
	    }
	}
	  protected void check(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		    String rightCode = (String)request.getSession().getAttribute("code");
		    String code = request.getParameter("code");
		    PrintWriter out = response.getWriter();
		    if (rightCode.equalsIgnoreCase(code)) {
		      out.print("ok");
		    }
		    else {
		      out.print("no");
		    }
		    out.close();
		  }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	protected void code(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("imgage/jpeg");
		// 禁止图片缓存
		response.setHeader("Pragma","no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0); 
		HttpSession session = request.getSession();
		ImageUtil imageUtil = new ImageUtil(120, 40, 5,30);
		session.setAttribute("code", imageUtil.getCode());
		imageUtil.write(response.getOutputStream());
	}
	//��֤��֤���Ƿ���ȷ
	private void checkCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String code = request.getParameter("code");
		HttpSession session = request.getSession();
		String rightCode = (String)session.getAttribute("code");
		PrintWriter printWriter = response.getWriter();
		if(code!=null&&code.equalsIgnoreCase(rightCode)) {
			printWriter.print("ok");
		}else {
			printWriter.print("fail");
		}
		printWriter.close();
	}

}
